﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; 

namespace DotNet.Utilities
{
    /// <summary>
    /// 提供加密/解密函数
    /// </summary>
    public class SecurityUtils
    {

        //默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="str">待加密的字符串</param>
        /// <param name="key">加密密钥(8位)</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源串</returns>
        public static string EncryptDES(string str, string key)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));

                byte[] rgbIV = Keys;

                byte[] inputByteArray = Encoding.UTF8.GetBytes(str);

                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();

                MemoryStream mStream = new MemoryStream();

                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

                cStream.Write(inputByteArray, 0, inputByteArray.Length);

                cStream.FlushFinalBlock();

                return Convert.ToBase64String(mStream.ToArray());

            }
            catch
            {
                return str;
            }

        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="str">待解密的字符串</param>
        /// <param name="key"> 解密密钥(8位),和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string DecryptDES(string str, string key)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));

                byte[] rgbIV = Keys;

                byte[] inputByteArray = Convert.FromBase64String(str);

                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();

                MemoryStream mStream = new MemoryStream();

                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

                cStream.Write(inputByteArray, 0, inputByteArray.Length);

                cStream.FlushFinalBlock();

                return Encoding.UTF8.GetString(mStream.ToArray());

            }
            catch
            {
                return str;
            }

        }
        /// <summary>
        /// MD5加密字符串
        /// </summary>
        /// <param name="str">源字符串</param>
        /// <returns>MD5加密后的字符串，失败返回源串</returns>
        public static string MD5String(string str)
        {
            try
            {
                MD5 md5 = new MD5CryptoServiceProvider();

                byte[] _byte = Encoding.Default.GetBytes(str);
                byte[] _byteRst = md5.ComputeHash(_byte);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < _byteRst.Length; i++)
                {
                    sb.Append(_byteRst[i].ToString("X2"));
                }
                return sb.ToString();
            }
            catch
            {
                return str;
            }
            //等效与
            //str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
        }
    }
}
